/**
 * @TODO: Check that all the attributes are set
 *
 */ 
var Badge = function(config) {
    if (typeof config.fontSize == "number") {
			this.fontSize = config.fontSize;
  	}else{
			throw new TypeError("fontSize is not a number");
		}
    if (typeof config.badgeX == "number") {
		  this.badgeX = config.badgeX;
  	}else{
			throw new TypeError("badgeX is not a number");	
		}
		if (typeof config.badgeY == "number") {
			this.badgeY = config.badgeY;
  	}else{
			throw new TypeError("badgeY is not a number");		
		}
		if (typeof config.badgeRadius == "number") {
			this.badgeRadius = config.badgeRadius;
  	}else{
			throw new TypeError("badgeRadius is not a number");	
		} 	
		if (typeof config.badgeColor == "string") {
		  this.badgeColor = config.badgeColor;
   	}else{
			throw new TypeError("badgeColor is not a string");	
		}
		if (typeof config.textColor == "string") {
		  this.textColor = config.textColor;
		}else{
			throw new TypeError("textColor is not a string");		
		}
}; 
Badge.fn = Badge.prototype = {

    draw : function (ctx, txt) {
    
        var text = txt;
        var textWidth = ctx.mozMeasureText(text);   

        ctx.save();            
        this.circle(ctx);
        //this.star(ctx);
        ctx.restore();        
        
        // Write the text
        ctx.translate(this.badgeX - (textWidth/2), 4 + this.fontSize/2);       
        ctx.save();
    

        ctx.fillStyle = "rgb(0,0,0)";
        ctx.translate(1, 1);       
        ctx.mozTextStyle = "bold normal "+ this.fontSize +"px Arial";
        ctx.mozDrawText(text);

        ctx.restore();
        ctx.fillStyle = this.textColor;
        ctx.mozTextStyle = "bold normal "+ this.fontSize +"px Arial";
        ctx.mozDrawText(text);           
        
        ctx.restore();
    },
    
    circle : function(ctx) {
        // Draw the shadow
        ctx.save();
                
        if (ctx.shadowOffsetX != null) {
            ctx.shadowOffsetX = 1;
            ctx.shadowOffsetY = 1;
            ctx.shadowBlur = 2;
            ctx.shadowColor = "rgba(0, 0, 0, 0.8)";        
        }else{
            ctx.beginPath();
            ctx.arc(this.badgeX - 1, this.badgeY + 1, this.badgeRadius, 0, Math.PI*2,true);      
            ctx.closePath();
            ctx.fillStyle = "rgb(0,0,0)";
            ctx.globalAlpha = 0.8;
            ctx.fill();  
        }
        
        // Draw the badge            
        ctx.beginPath();
        ctx.arc(this.badgeX, this.badgeY, this.badgeRadius, 0, Math.PI*2,true);
        ctx.closePath();
        ctx.fillStyle = this.badgeColor;                       
        ctx.globalAlpha = 1;
        ctx.fill();      

        ctx.restore();
        
        // Draw the gradient            
        ctx.beginPath();
        ctx.arc(this.badgeX, this.badgeY, this.badgeRadius, 0, Math.PI*2,true);
        var lingrad = ctx.createLinearGradient(this.badgeX - this.badgeRadius,this.badgeY - this.badgeRadius, this.badgeRadius*2, this.badgeRadius*2);
        lingrad.addColorStop(0, 'rgba(0,0,0,0.1)');
        lingrad.addColorStop(0.5, 'rgba(256,256,256,0.6)');
        ctx.closePath();
        ctx.fillStyle = lingrad;                       
        ctx.fill();		

        ctx.globalAlpha = 1;
    },
    
    star : function(ctx) {
        var shape = function(ctx,r,spikes){
          ctx.save();
          ctx.beginPath()
          ctx.moveTo(r-1,0);
          for (i=0;i<(spikes*2)-1;i++){
            ctx.rotate(Math.PI/spikes);
            if(i%2 == 0) {
              ctx.lineTo((r+1),0);
            } else {
              ctx.lineTo(r-1,0);
            }
          }
          ctx.closePath();
          ctx.restore();
        };
        
        ctx.translate(this.badgeX, this.badgeY);

        ctx.save();
        
        if (0 && ctx.shadowOffsetX != null) {
            ctx.shadowOffsetX = 1;
            ctx.shadowOffsetY = 1;
            ctx.shadowBlur = 2;
            ctx.shadowColor = "rgba(0, 0, 0, 0.8)";        
        }else{
            ctx.translate(-1, 1);
            shape(ctx, this.badgeRadius,10);
            ctx.fillStyle = "rgb(256,256,256,0.9)";                       
            ctx.fill();
            ctx.restore();
        }
            
        shape(ctx, this.badgeRadius,10);
        ctx.fillStyle = this.badgeColor;                       
        ctx.fill();

        shape(ctx, this.badgeRadius,10);
        var lingrad = ctx.createLinearGradient(this.badgeX - this.badgeRadius,this.badgeY - this.badgeRadius, this.badgeRadius*2, this.badgeRadius*2);
        lingrad.addColorStop(0, 'rgba(0,0,0,0.1)');
        lingrad.addColorStop(0.5, 'rgba(256,256,256,0.8)');
        ctx.fillStyle = lingrad;                       
        ctx.fill();

    }
    
    
}
